//
//  CarouselFigure.m
//  轮播图
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CarouselFigure.h"

@implementation CarouselFigure

-(void)dealloc
{
    [_scrView release];
    [_timer release];
    [_page release];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrView = [[UIScrollView alloc] initWithFrame:frame];
        //水平滚动条隐藏
        _scrView.showsHorizontalScrollIndicator = NO;
        //边缘弹动效果关
        _scrView.bounces = NO;
        //以当前滚动视图宽度为一页
        _scrView.pagingEnabled = YES;
        [self addSubview:_scrView];
        [_scrView release];
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        //_page.backgroundColor = [UIColor blackColor];
        [self addSubview:_page];
        [_page release];
        _page.center = CGPointMake(self.center.x, self.frame.size.height - 50);
        [_page addTarget:self action:@selector(page:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

#pragma mark - 添加图片

-(void)setImages:(NSMutableArray *)imgArr
{
    
    [imgArr addObject:[imgArr firstObject]];
    NSLog(@"%ld",imgArr.count);
    //滚动内容大小
    self.scrView.contentSize = CGSizeMake(_scrView.frame.size.width * (imgArr.count- 1), 0);
    for (NSInteger i = 0; i < imgArr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(_scrView.frame.size.width * i, 0, _scrView.frame.size.width, _scrView.frame.size.height)];
        imgView.image = imgArr[i];
        [_scrView addSubview:imgView];
        [imgView release];
        
    }
    self.page.numberOfPages = imgArr.count - 1;
    [self createTimer];
    
    self.scrView.contentOffset = CGPointMake(0, 0);
}
#pragma mark - 创建定时器
-(void)createTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

#pragma mark - 定时器方法
-(void)timerAction:(NSTimer *)timer
{
    
    _imgPage++;
    
    //[UIView animateWithDuration:0.5 animations:^{
        self.scrView.contentOffset = CGPointMake(_scrView.contentOffset.x + self.frame.size.width, 0);
        if (_scrView.contentOffset.x >= (_scrView.contentSize.width + self.frame.size.width ) ) {
            _scrView.contentOffset = CGPointZero;
            _imgPage = 0;
        }
        self.page.currentPage = _scrView.contentOffset.x / self.frame.size.width;
    //}];
}


-(void)page:(UIPageControl *)page
{
    //通过动画滚动（自定义）
    [UIView animateWithDuration:0.5 animations:^{
        self.scrView.contentOffset = CGPointMake(375 * page.currentPage, 0);
    }];
}
#pragma mark - 视图将要开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}
#pragma mark - 视图已经结束拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
@end
